import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        /**
         * 是否需要强制登录
         */
		usertype: 2,
        forcedLogin: false,
        hasLogin: false,
        userName: "",
		token: '',
		user: {
			uid: '',
			keyid: 0,
			token:'',
			timer:null,
			userimg:'',
			sex:'',
			email:'',
		},
		fromlocal: true,
		fromz3950: true,
		Quizetask: 0,
		examtask: 0,
		reviewtask:0,
		writingtask: 0
    },
	getters:{
		testCount(state){
			const num = state.Quizetask + state.examtask
			return num
		},
		writtingCount(state){
			const num = state.reviewtask + state.writingtask
			return 0
		}
	},
    mutations: {
		setImg(state, url){
		   state.user.userimg = url
	     },
		 setToken(state, token){
			 state.token = token
		 },
		setInfo(state,info){
		info.uid = info.id
		state.user = info;
		uni.setStorageSync('user', info)		
		// state.user.sex=info.sex
		// state.user.email=info.email
		},
		setInitialtesting(state, initialtesting){
			state.user.initialtesting = initialtesting
			uni.setStorageSync('user', state.user)
		},
        login(state, user) {
            state.user = user;
            state.hasLogin = true;
			uni.setStorageSync('user', user)
        },
        logout(state) {
            state.user = {};
            state.hasLogin = false;
			uni.clearStorageSync();
        },
		setQuizetask(state, count){
			state.Quizetask = count
		},
		reduceQuizetask(state){
			state.Quizetask = state.Quizetask - 1
		},
		setExamtask(state, count){
			state.examtask = count
		},
		reduceExamtask(state){
			state.examtask = state.examtask - 1
		},
		setReviewtask(state, count){
			state.reviewtask = count
		},
		reduceReviewtask(state){
			state.reviewtask = state.reviewtask - 1
		},
		setWritingtask(state, count){
			state.writingtask = 0
		},
		reduceWritingtask(state){
			state.writingtask = state.writingtask - 1
		}
    },
	
	  actions: {
	    // 设置用户头像
	    setImg({ commit }, url) {
	      commit('setImg', url)
	    },
		// 信息修改
		setInfo({ commit },info){
			commit('setInfo',info)
		},
		setToken({commit}, token){
			commit('setToken', token)
		},
		//退出登录
		logout({commit}){
			commit('logout')
		}
		}
})

export default store
